package com.softeem.music_sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softeem.music_sys.comm.Result;
import com.softeem.music_sys.entity.Music;
import com.softeem.music_sys.service.MusicService;
import com.softeem.music_sys.mapper.MusicMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author RD
* @description 针对表【music(音乐信息表)】的数据库操作Service实现
* @createDate 2025-11-07 11:19:17
*/
@Service
public class MusicServiceImpl extends ServiceImpl<MusicMapper, Music> implements MusicService{

    @Override
    public Page<Music> getMusicsByAlbum(Integer albumId, Integer page, Integer limit) {
        Page<Music> pageUtil = new Page<>(page, limit);
        QueryWrapper<Music> wrapper = new QueryWrapper<>();
        wrapper.eq("album_id", albumId);
        wrapper.eq("status", 1);
        wrapper.orderByDesc("release_time");
        return page(pageUtil, wrapper);
    }

    @Override
    public Page<Music> searchMusic(String keyword, Integer page, Integer limit) {
        Page<Music> pageUtil = new Page<>(page, limit);
        QueryWrapper<Music> wrapper = new QueryWrapper<>();
        wrapper.like("name", keyword);
        wrapper.eq("status", 1);
        wrapper.orderByDesc("play_count");
        return page(pageUtil, wrapper);
    }

    @Override
    public List<Music> getHotMusic(Integer limit) {
        QueryWrapper<Music> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1);
        wrapper.orderByDesc("play_count");
        wrapper.last("limit " + limit);
        return list(wrapper);
    }

    @Override
    public List<Music> getNewMusic(Integer limit) {
        QueryWrapper<Music> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1);
        wrapper.orderByDesc("release_time");
        wrapper.last("limit " + limit);
        return list(wrapper);
    }

    @Override
    public boolean increasePlayCount(Integer musicId) {
        Music music = getById(musicId);
        if (music != null) {
            music.setPlayCount(music.getPlayCount() + 1);
            return updateById(music);
        }
        return false;
    }
    
    @Override
    public Result addMusic(Music music) {
        // 设置默认状态和播放次数
        music.setStatus(1);
        if (music.getPlayCount() == null) {
            music.setPlayCount(0L);
        }
        
        boolean result = this.save(music);
        return result ? Result.success().setMessage("添加成功") : Result.fail("添加失败");
    }
    
    @Override
    public Result getMusicById(Integer id) {
        Music music = this.getById(id);
        if (music == null) {
            return Result.fail("音乐不存在");
        }
        return Result.success().setData(music);
    }
    
    @Override
    public Result updateMusic(Music music) {
        Music existMusic = this.getById(music.getId());
        if (existMusic == null) {
            return Result.fail("音乐不存在");
        }
        boolean result = this.updateById(music);
        return result ? Result.success().setMessage("更新成功") : Result.fail("更新失败");
    }
    
    @Override
    public Result deleteMusic(Integer id) {
        Music music = this.getById(id);
        if (music == null) {
            return Result.fail("音乐不存在");
        }
        boolean result = this.removeById(id);
        return result ? Result.success().setMessage("删除成功") : Result.fail("删除失败");
    }
    
    @Override
    public Result getMusicList(Integer page, Integer limit) {
        Page<Music> pageUtil = new Page<>(page, limit);
        QueryWrapper<Music> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1);
        Page<Music> resultPage = this.page(pageUtil, wrapper);
        return Result.success().setData(resultPage);
    }
    
    @Override
    public Result searchMusicController(String name, Integer page, Integer limit) {
        Page<Music> pageUtil = new Page<>(page, limit);
        QueryWrapper<Music> wrapper = new QueryWrapper<>();
        wrapper.like("name", name);
        wrapper.eq("status", 1);
        Page<Music> resultPage = this.page(pageUtil, wrapper);
        return Result.success().setData(resultPage);
    }
    
    @Override
    public Result getMusicsByAlbumController(Integer albumId, Integer page, Integer limit) {
        Page<Music> pageUtil = new Page<>(page, limit);
        QueryWrapper<Music> wrapper = new QueryWrapper<>();
        wrapper.eq("album_id", albumId);
        wrapper.eq("status", 1);
        Page<Music> resultPage = this.page(pageUtil, wrapper);
        return Result.success().setData(resultPage);
    }
    
    @Override
    public Result getHotMusicController(Integer page, Integer limit) {
        Page<Music> pageUtil = new Page<>(page, limit);
        QueryWrapper<Music> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1);
        wrapper.orderByDesc("play_count");
        Page<Music> resultPage = this.page(pageUtil, wrapper);
        return Result.success().setData(resultPage);
    }
    
    @Override
    public Result getNewMusicController(Integer page, Integer limit) {
        Page<Music> pageUtil = new Page<>(page, limit);
        QueryWrapper<Music> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1);
        wrapper.orderByDesc("release_time");
        Page<Music> resultPage = this.page(pageUtil, wrapper);
        return Result.success().setData(resultPage);
    }
    
    @Override
    public Result playMusicController(Integer id) {
        Music music = this.getById(id);
        if (music == null) {
            return Result.fail("音乐不存在");
        }
        if (music.getStatus() == 0) {
            return Result.fail("音乐已下架");
        }
        
        // 播放次数+1
        music.setPlayCount(music.getPlayCount() + 1);
        boolean result = this.updateById(music);
        return result ? Result.success().setMessage("播放成功").setData(music) : Result.fail("播放失败");
    }
    
    @Override
    public Result updateStatusController(Integer id, Integer status) {
        Music music = this.getById(id);
        if (music == null) {
            return Result.fail("音乐不存在");
        }
        music.setStatus(status);
        boolean result = this.updateById(music);
        return result ? Result.success().setMessage("状态更新成功") : Result.fail("状态更新失败");
    }
}